package sqldbManagement.dbparklot.cj.utils;

import java.io.FileInputStream;
import java.sql.*;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public final class JDBCUtil {

//    private final static String path = "resources/jdbc.properties";
//
//    private static String url;
//    private static String driver;
//    private static String username;
//    private static String password;
//
//    static {
//        try {
//            ResourceBundle rb =  new PropertyResourceBundle(new FileInputStream(
//                    JDBCUtil.class.getClassLoader().getResource(".").getPath() +
//                            path));
//
//            url = rb.getString("url");
//            driver = rb.getString("driver");
//            username = rb.getString("username");
//            password = rb.getString("password");
//
//            Class.forName(driver);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
	static ResourceBundle rb = ResourceBundle.getBundle("sqldbManagement.dbparklot.jdbc");
	static String url = rb.getString("url");
	static String driver = rb.getString("driver");
	static String user = rb.getString("user");
	static String pass = rb.getString("pass");
	
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

    /**
     * 获取链接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,pass);
    }

    /**
     * 关闭连接和语句对象
     * @param conn
     * @param ps
     */
    public static void close(Connection conn, Statement ps) {
        close(conn,ps,null);
    }

    /**
     * 关闭所有连接
     * @param conn
     * @param ps
     * @param rs
     */
    public static void close(Connection conn, Statement ps, ResultSet rs) {
        try {

            if(rs != null)
                rs.close();

            if(ps != null)
                ps.close();

            if(conn != null)
                conn.close();

            rs = null;
            ps = null;
            conn = null;
        } catch (SQLException e) {
            // TODO: handle exception
        }
    }

}